package com.regiee.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regiee.common.R;
import com.regiee.entity.Employee;
import com.regiee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /*
    登录接口
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password=employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());//对密码进行MD5加密处理
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<Employee>()
                .eq(Employee::getUsername,employee.getUsername());
        Employee employee1=employeeService.getOne(wrapper);
        if(employee1==null){
            return R.error("账号不存在");
        }
        if(!employee1.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if(employee1.getStatus()==0){
            return R.error("账号已经禁用");
        }
        //登录成功，把employee的id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",employee1.getId());
        return R.success(employee1);
    }
    /*
    退出接口
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清除用户ID
         request.getSession().removeAttribute("employee");
         return R.success("退出成功");
    }
    /*
    新增员工接口
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest request){
        //设置初始密码为123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //剩余字段值在此添加,时间以及创建人
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        //获取当前登录的用户
//        Long empId=(Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }
    /*
    员工分页查询接口
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(int page,int pageSize,String name){
        //构建分页条件以及分页条件
        Page<Employee> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<Employee>().
                like(StringUtils.isNotEmpty(name),Employee::getName,name)//过滤条件，判断模糊查询框是否有输入值
                .orderByDesc(Employee::getUpdateTime);
        //进行查询
        employeeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
    /*
    修改员工
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request){
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("员工修改成功");
    }
    /*
    通过ID来获取员工信息
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee=employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("没有找到该员工");
    }
}
